class Solution {
    public int search(int[] nums, int target) {
        // 还是照着模板写吧，不然总有漏掉的时候，尴尬呀
        // 条件 nums[i] <= target
        int left = -1;
        int right = nums.length - 1;
        int ans1 = right;
        int mid = 0;
        while (left < right) {
            mid = (left + right + 1) >> 1;
            if (nums[mid] <= target) {
                if (nums[mid] == target) return mid;
                left = mid;
            } else {
                right = mid - 1;
            }
        }
        ans1 = right;
        // 条件 nums[i] >= target
        left = 0;
        right = nums.length;
        while (left < right) {
            mid = (left + right) >> 1;
            if (nums[mid] >= target) {
                right = mid;
            } else {
                left = mid + 1;
            }
        }
        return right == ans1 ? right : -1;
    }
}
